package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdPage;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdPageService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chart/page")
public class AdPageController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdPageController.class);

    @Reference
    private AdPageService adPageService;

    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse get(HttpServletRequest request) {

        List<AdPage> adPages = adPageService.selectAdPages();
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.listToListMap(adPages));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse search(HttpServletRequest request) {
        String criteria = request.getParameter("criteria");
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        PageInfo pageInfo = adPageService.selectAdPagesPaged(criteria, pageIndex, pageSize);

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(pageInfo.getList()));
        map.put("count", pageInfo.getTotal());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        String pageIds = request.getParameter("pageIds");
        JSONArray jsonArray = (JSONArray) JSON.parse(pageIds);
        ArrayList<Integer> ids = new ArrayList<>();
        if (jsonArray != null) {
            int len = jsonArray.size();
            for (int i = 0; i < len; i++) {
                ids.add(jsonArray.getInteger(i));
            }
        }
        adPageService.deleteByPrimaryKeys(ids);
        return JsonResponse.success();
    }


    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        String pageName = request.getParameter("pageName");
        String pageHome = request.getParameter("pageHome");
        Integer folderId = Integer.parseInt(request.getParameter("folderId"));
        String pageTypeId = request.getParameter("pageTypeId");

        AdPage adPage = new AdPage();
        adPage.setPageName(pageName);
        adPage.setPageHome(pageHome.toLowerCase().equals("true"));
        adPage.setPageFolderId(folderId);
        adPage.setPageTypeId(pageTypeId);
        adPage.setPageVersion(1);
        adPage.setPageData("");

        adPage.setPageId(adPageService.insert(adPage));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adPage));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/load", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse load(HttpServletRequest request) {

        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        AdPage adPage = adPageService.selectByPrimaryKey(pageId);

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adPage));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/refresh", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse refresh(HttpServletRequest request) {

        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        AdPage adPage = adPageService.selectByPrimaryKey(pageId);

        AdPage adPageUpdate = new AdPage();
        adPageUpdate.setPageId(pageId);
        adPageUpdate.setPageVersion(adPage.getPageVersion() + 1);

        adPageService.updateByPrimaryKeySelective(adPageUpdate);

        JsonResponse jsonResponse = new JsonResponse();

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update(HttpServletRequest request) {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        String pageName = request.getParameter("pageName");
        String pageData = request.getParameter("pageData");
        String pageHome = request.getParameter("pageHome");
        Integer folderId = Integer.parseInt(request.getParameter("folderId"));
        Integer pageVersion = Integer.parseInt(request.getParameter("pageVersion"));
        String pageTypeId = request.getParameter("pageTypeId");

        AdPage adPage = new AdPage();
        adPage.setPageId(pageId);
        adPage.setPageName(pageName);
        adPage.setPageHome(pageHome.toLowerCase().equals("true"));
        adPage.setPageData(pageData);
        adPage.setPageFolderId(folderId);
        adPage.setPageTypeId(pageTypeId);
        adPage.setPageVersion(pageVersion + 1);

        adPageService.updateByPrimaryKeySelective(adPage);

        adPage = adPageService.selectByPrimaryKey(pageId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adPage));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


}
